/**
 *
 */
package jp.seraph.same.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import jp.seraph.same.controller.PoseController;
import jp.seraph.same.model.PoseCollectionModel;
import jp.seraph.same.model.PoseCollectionModelListener;
import jp.seraph.same.model.PoseModel;

/**
 * ポーズを編集するのに使用されるパネル Pose関係パネルはすべて、このパネル以下の階層に配置される。
 * 
 */
public class PosePanel extends JPanel implements PoseCollectionModelListener {
    public PosePanel(PoseCollectionModel aModel, PoseController aController) {
        mModel = aModel;
        mController = aController;
        mSaveButton = new JButton("保存");
        mDeleteButton = new JButton("削除");
        mRealTimeCheckBox = new JCheckBox("ポーズ反映");
        mReverseButton = new JButton("左右反転");
        mLeftToRightButton = new JButton("左->右");
        mRightToLeftButton = new JButton("左<-右");
        mPoseNameBox = new JTextField();
        mJointPanel = new JointPanel(mModel.getSelectedPose(), mController);
        mPoseListPanel = new PoseListPanel(mModel, mController);
        mModel.addListener(this);

        initializeComponent();
        initializeEvent();
    }

    private void initializeComponent() {
        mRealTimeCheckBox.setSelected(true);

        this.setSize(DEFAULT__WIDTH, DEFAULT__HEIGHT);
        this.setLayout(new GridLayout(1, 3));

        JPanel tSavePanel = new JPanel();
        // tSavePanel.setLayout(new GridLayout(8, 1));
        // tSavePanel.setLayout(new BoxLayout(tSavePanel, BoxLayout.Y_AXIS));
        tSavePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        // tSavePanel.setLayout(null);
        tSavePanel.setPreferredSize(new Dimension(100, 30));
        mPoseNameBox.setPreferredSize(new Dimension(100, 21));
        mSaveButton.setPreferredSize(new Dimension(100, 21));
        mDeleteButton.setPreferredSize(new Dimension(100, 21));
        mReverseButton.setPreferredSize(new Dimension(100, 21));
        mLeftToRightButton.setPreferredSize(new Dimension(100, 21));
        mRightToLeftButton.setPreferredSize(new Dimension(100, 21));

        tSavePanel.add(mPoseNameBox);
        tSavePanel.add(mSaveButton);
        tSavePanel.add(mDeleteButton);
        tSavePanel.add(mRealTimeCheckBox);
        tSavePanel.add(mReverseButton);
        tSavePanel.add(mLeftToRightButton);
        tSavePanel.add(mRightToLeftButton);

        this.add(mJointPanel);
        this.add(tSavePanel);
        this.add(mPoseListPanel);
    }

    private void initializeEvent() {
        mSaveButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent aActionevent) {
                mController.savePose(mPoseNameBox.getText(), mJointPanel.createPose());
            }
        });

        mDeleteButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                mController.removePose(mModel.getSelectedPoseName());
            }
        });

        mRealTimeCheckBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                mJointPanel.setRealTimeUpdate(mRealTimeCheckBox.isSelected());
            }
        });

        mReverseButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent aE) {
                mController.reversePoseAsMirror();
                if (mRealTimeCheckBox.isSelected())
                    mController.updatePose();
            }
        });

        mLeftToRightButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent aE) {
                mController.copyLeftToRight();
                if (mRealTimeCheckBox.isSelected())
                    mController.updatePose();
            }
        });

        mRightToLeftButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent aE) {
                mController.copyRightToLeft();
                if (mRealTimeCheckBox.isSelected())
                    mController.updatePose();
            }
        });
    }

    private PoseCollectionModel mModel;
    private PoseController mController;
    private JButton mSaveButton;
    private JButton mDeleteButton;
    private JButton mReverseButton;
    private JButton mLeftToRightButton;
    private JButton mRightToLeftButton;
    private JCheckBox mRealTimeCheckBox;
    private JTextField mPoseNameBox;
    private JointPanel mJointPanel;
    private PoseListPanel mPoseListPanel;
    private static final long serialVersionUID = 10L;
    private static final int DEFAULT__WIDTH = 500;
    private static final int DEFAULT__HEIGHT = 400;

    public void PoseCountChanged(PoseCollectionModel aSender) {
        // 何もしない
    }

    public void SelectedPoseChanged(PoseCollectionModel aSender, int aIndex, String aPoseName) {
        PoseModel tPose = aSender.getSelectedPose();
        mJointPanel.setModel(tPose);
    }

}
